//app.js

const util = require("./utils/config.js");
//index.js
//获取应用实例
const app = getApp()
const http_host = util.http_host;
const agent_id = util.agent_id;
const img_url = util.img_url;
const appId = util.appId;
const secret = util.secret;

App({
  onLaunch: function () {




    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    var that = this;
    // 登录
    wx.login({
      success: res => {

        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })

    // 获取用户信息
    wx.getSetting({

      success: res => {

        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {


              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
    wx.login({


      success: function (res) {
        if (res.code) {
          wx.request({
            url: http_host + 'getMemberOpenid',
            data: {
              appId: appId,
              secret: secret,
              code: res.code
            },
            header: {
              'Content-Type': 'application/json'
            },
            success: function (res) {
              var openid = res.data.openid
              console.log(666)
              console.log(openid)
              that.globalData.openId = openid
              typeof cb == "function" && cb(that.globalData.openId)
              wx.getUserInfo({
                success: function (res) {
                  wx.request({
                    url: http_host + 'inuser',
                    data: {
                      // data:res.userInfo
                      openid: openid,
                      nickname: res.userInfo.nickName,
                      headimgurl: res.userInfo.avatarUrl,
                      province: res.userInfo.province,
                      state: 0,
                      sex: res.userInfo.gender,
                      status: 0,
                      agents_id: agent_id,
                    },
                    header: {
                      'Content-Type': 'application/json'
                    },
                    success: function (res) {
                    }
                  })
                }
              })
            }
          })
        }
      }
    })
    //获取到当前小程序的access_token
    wx.request({
      url: http_host + 'getAccessToken',
      data: {
        appId: appId,
        secret: secret,
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        that.globalData.accessToken = res.data
      }
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '如皋人气美食',
      desc: '如皋人气美食',
      path: '/pages/index'
    }
  },
  globalData: {
    userInfo: null,
    openId: '',
    goods: '',
    shopId: '',
    accessToken: '',
    is_free: '',
    order: '',

  },
  address: {
    lat: '',
    lng: '',
  },
})
